package com.king.juc.syncmode;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;

/**
 * 同步模式-保护性暂停
 * @author king
 * @date 2020/11/5 14:44
 */
@Slf4j(topic = "c.ProtectiveSuspension")
public class ProtectiveSuspension {
    public static void main(String[] args) {
        GuardedObject guardedObject = new GuardedObject();
        new Thread(() -> {
            // 等待结果
            log.debug("等待结果");
//            List<String> list = (List<String>)guardedObject.get();
//            log.debug("结果: [{}] \n size: [{}]", list, list.size());
            Object response = guardedObject.getByTimeOut(2000);
            log.debug("结果是: [{}]", response);
        }, "t1").start();

        new Thread(() -> {
            log.debug("执行下载");
            try {
                List<String> download = Downloader.download();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                guardedObject.complete(download);
                guardedObject.complete(null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }, "t2").start();
    }
}
